<?php
require_once(ROOT . LIBRARY .'/FirePHP.class.php');

/**
 * Error handler
 *
 * @author snow
 */
class error
{
   private $FIREBUG = NULL;
   private $debug   = FALSE;
   private $errors  = array();
   /**
    * ERROR VARIABLE FORMAT:
    * status => Error status FALSE || TRUE
    * varkey => Language variable for error message
    * code   => Error code ID
    * data   => Return working on variables
    * exec   => Execute JS function on Client side
    * menu   => array('varkey'=>'href');
    */
   private $error = array('status'=>FALSE,'varkey'=>'unknown','code'=>FALSE,'data'=>FALSE,'exec'=>FALSE,'menu'=>FALSE,'text'=>FALSE);
   /**
    * SUCCES EXECUTE FORMAT:
    * status => Execute status FALSE || TRUE
    * varkey => Language variable for success execution
    * code   => Success code ID
    * exec   => Execute JS function on Client side
    * result => results of execution (values)
    * menu   => array('varkey'=>'href');
    */
   private $success = array('status'=>FALSE,'varkey'=>'saved','code'=>FALSE,'exec'=>FALSE,'result'=>FALSE,'menu'=>FALSE);

   private function  __construct()
   {
      $this->FIREBUG = FirePHP::getInstance(true);
      $this->debug = MODE_DEBUG;
      if(MODE_DEBUG)
      {
         ini_set('display_errors', 1);
      }
   }

   static public function getInstance($params = array())
   {
      static $object_store_singelton;
      if(!is_object($object_store_singelton))
      {
         $object_store_singelton = new error();
      }
      return $object_store_singelton;
   }

   public function setError($params = array(), $level = 4)
   {
      if($this->debug)
      {
         switch ($level)
         {
            case 1:
               $error_level = E_USER_NOTICE;
               break;
            case 2:
               $error_level = E_USER_WARNING;
               break;
            case 3:
               $error_level = E_USER_ERROR;
               break;
         }

         if($level < 4 && isset($params['message']))
         {
            trigger_error($params['message'], $error_level);
         }
      } else
      {
         // LOG ERRORS
      }

      $this->error['status'] = TRUE;
      $this->error['varkey'] = !empty($params['varkey'])  ? $params['varkey']  : FALSE;
      $this->error['code']   = !empty($params['code'])    ? $params['code']    : FALSE;
      $this->error['data']   = !empty($params['data'])    ? $params['data']    : FALSE;
      $this->error['exec']   = !empty($params['exec'])    ? $params['exec']    : FALSE;
      $this->error['menu']   = !empty($params['menu'])    ? $params['menu']    : FALSE;
      $this->error['text']   = !empty($params['message']) ? $params['message'] : '';

      $code = !empty($params['code']) ? $params['code'] : 'common';
      $this->errors[$code][]        = $this->error;
   }


   public function setSuccess($params = array())
   {
      $this->success['status'] = TRUE;
      $this->success['varkey'] = !empty($params['varkey']) ? $params['varkey'] : FALSE;
      $this->success['code']   = !empty($params['code'])   ? $params['code']   : FALSE;
      $this->success['exec']   = !empty($params['exec'])   ? $params['exec']   : FALSE;
      $this->success['result'] = !empty($params['result']) ? $params['result'] : FALSE;
      $this->success['menu']   = !empty($params['menu'])   ? $params['menu']   : FALSE;
      $this->success['text']   = !empty($params['text'])   ? $params['text']   : FALSE;
   }


   /**
    * Return Error status by its code, or anyone
    * 
    * @param string $code
    * @return bool
    */
   public function getErrorStatus($code = NULL)
   {
      if($code && isset($this->errors[$code]))
      {
         return TRUE;
      } elseif($code)
      {
         return FALSE;
      }

      return $this->error['status'];
   }


   /**
    * Get array of errors
    *
    * @param string $code
    * @return array
    */
   public function getErrors($code = NULL)
   {
      if($code && !empty($this->errors[$code]))
      {
         return $this->errors[$code];
      } elseif(!$code && !empty($this->errors))
      {
         $errors = array();
         foreach($this->errors AS $e)
         {
            $errors = array_merge($errors, $e);
         }
         return $errors;
      }
      
      return FALSE;
   }


   /**
    * Get Last error (common or in given Code)
    *
    * @param string $code
    * @return array
    */
   public function getLastError($code = NULL)
   {
      if($code && isset($this->errors[$code]))
      {
         return end($this->errors[$code]);
      } elseif(!$code && $this->error['status'])
      {
         return $this->error;
      }

      return FALSE;
   }

   public function getSuccess()
   {
      if($this->success['status'] == TRUE) return $this->success;
      return FALSE;
   }

   public function dump($name, $value)
   {
      $this->FIREBUG->log($value, $name);
   }
}